<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAdminOperationLogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admin_operation_logs', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('admin_id')->default(0)->comment('管理員ID');
            $table->integer('staff_id')->default(0)->comment('工號');
            $table->string('admin_name', 60)->default('')->comment('管理員');
            $table->string('route_name', 50)->default('')->comment('路由名');
            $table->string('route_permission', 100)->default('')->comment('權限名');
            $table->string('method', 10)->default('')->comment('請求方法');
            $table->string('uri')->default('')->comment('URI');
            $table->string('url')->default('')->comment('URL');
            $table->string('full_url')->default('')->comment('完整URL');
            $table->ipAddress('ip')->default('')->comment('IP');
            $table->string('session_id', 64)->default('')->comment('會話ID');
            $table->json('query')->nullable()->comment('請求參數');
            $table->string('user_agent')->default('')->comment('瀏覽器信息');
            $table->timestamp('operated_at')->useCurrent()->comment('操作時間');

            $table->index(['operated_at', 'staff_id'], 'oasi');
            $table->index(['operated_at', 'uri'], 'oau');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admin_operation_logs');
    }
}
